define([
        'jquery'
 
        
   
        ], function ($){
	
	
	var MemoView = function() {
		var _this = this;
		
		this.drawing = function(id) {		
			
			var $mainDiv = $('#main');
			$mainDiv.html("");
			
			var $mainHeader = $('<header>', {id : "mainHeader"}).appendTo($mainDiv);
			var $mainNav = $('<nav>', {id : "mainNav"}).appendTo($mainDiv);
			var $mainArticle = $('<article>', {id : "mainArticle"}).appendTo($mainDiv);
			
			var $h1 = $('<h1>').appendTo($mainHeader);
			$h1.html(id+"님의 메모장입니다.");
			
			
			var $folderDiv = $('<div>', {id : "folder"}).appendTo($mainNav);
			////////////////////////  폴더 섹션
							
			var $folderHeader = $('<header>', {id : "folderHeader"}).appendTo($folderDiv);
			var $folderArticle = $('<article>', {id : "folderArticle"}).appendTo($folderDiv);
	
			var $folderFooter = $('<footer>', {id : "folderFooter"}).appendTo($folderDiv);

			this.$createFolder = $('<input>', {id : "createFolder", value : "폴더 추가",type : "button", class : "memoBtn"}).appendTo($folderHeader);
		
			this.$allFolder = $('<input>', {id : "allFolder", type : "button", value : "전체", class : "memoBtn"}).appendTo($folderHeader);


			/////////////////////////////   메모 리스트 섹션
			
			var $memoDiv = $('<div>', {id : "memo"}).appendTo($mainNav);
			
			var $memoHeader = $('<header>', {id : "memoHeader"}).appendTo($memoDiv);
			var $memoArticle = $('<article>', {id : "memoArticle"}).appendTo($memoDiv);
		
			var $memoFooter = $('<footer>', {id : "memoFooter"}).appendTo($memoDiv);
			
			
			this.$createMemo = $('<input>', {id : "createMemo", value : "메모 추가", type : "button", class : "memoBtn", folder_data : null}).appendTo($memoHeader);
			
			
			var $search = $('<input>', {type : "search", placeholder : "search..."}).appendTo($memoHeader);


			
			///////////////////////////////////////////// 메모 내용 섹션

			var $memoContentDiv = $('<div>', {id : "memoContent"}).appendTo($mainArticle);

			var $memoContentHeader = $('<header>', {id : "memoContentHeader"}).appendTo($memoContentDiv);
			var $memoContentArticle = $('<article>', {id : "memoContentArticle"}).appendTo($memoContentDiv);
			
			
			
			this.$reset = $('<input>', {id : "deleteMemoContent", value : "reset!", type : "button", class : "memoBtn"}).appendTo($memoContentHeader);
		 
			this.$save = $('<input>', {id : "saveMemoContent", value : "저장", type : "button", class : "memoBtn"}).appendTo($memoContentHeader);
			
			
			var $content = $('<div>', {id : "content"}).appendTo($memoContentArticle);
			
			var $textarea = $('<textarea>', {id : "text", rows : "35", cols : "35"}).appendTo($content);
			///////////////////////////////////////////////
			
			this.$imageDiv =$('<img id = "image" src = "./CSS/logo.png" />').insertBefore($('header')[0]);
	
			
			this.$input1 = $('<input>',{type : "button", value : '회원정보 수정', class : "title"}).appendTo($mainHeader);
			this.$input2 = $('<input>',{type : "button", value : '로그아웃', class : "title"}).appendTo($mainHeader);
			this.$input3 = $('<input>',{type : "button", value : '회원탈퇴', class : "title"}).appendTo($mainHeader);
		
		};

		this.addMemo = function() {
			var result = null;
			var $createMemo =  $('#createMemo');
			if($createMemo.attr("folder_data") == null) {
				result =  null;
		
			}else {
				var params = "folder_num="+$createMemo.attr("folder_data");   //메모 생성 버튼 속성에 폴더이름 추가
				result =  params;
			}
			return result;
			
		};
		this.drawMemo = function(memo, removeBtnEvent) {
			var $memoArticle = $('#memoArticle');
			var memoContent = null;
			
			if(memo.getMemoContent().split("\n")[0].length < 10) {
				
				memoContent = memo.getMemoContent().split("\n")[0];

			}else {
			
				memoContent = memo.getMemoContent().slice(0,10);

			}
			
			_this.$newMemo = $('<input>', {type : "button", value :   (memo.getMemoContent())? memoContent : "새 메모"   ,class : "memoList", memo_id : (memo.getMemoNum())? memo.getMemoNum() : ""}).appendTo($memoArticle);
			
			_this.$newMemo.on('click',function(e) { //한번 클릭했을 때 메모 내용 보여주기
				
				
				
				_this.selectMemo(memo.getMemoNum() ,e, removeBtnEvent); 
				_this.removeText();
				_this.addText(null, memo.getMemoNum(), memo.getMemoContent() ,e);  
				
			
			});
	
		
		};
		this.selectMemo = function(memoId, e, removeBtnEvent) {    
			var memoBtn = e.target;
			$('.click').removeClass("click");
			$(e.target).addClass("click");
			
			$('.removeMemoBtn').remove();
			
			this.$removeMemo = $('<input>', {type : "button", id :  "removeMemoBtn", value :  "X", class : "removeMemoBtn", dataTarget : memoId/*visibility : "hidden" */}).insertAfter($(memoBtn));
			this.$removeMemo.on('click',removeBtnEvent);    //삭제 버튼 생성
		
			
		};
		this.removeText = function() {  
		
			$('#text').val("");
			
			
		};
		this.addText = function(updatedContent,memoId, memoContent, e) {//////////////////////////////////
			
			if(updatedContent == null) {
				
				var $text = $('#text');	
				$text.attr("memo_id", memoId);
				$text.val( memoContent);
				
			}else {
				
				
				var $text = $('#text');	
				$text.attr("memo_id", memoId);
				$text.val( updatedContent);
			}
			
			
		
		};

//		this.saveMemo = function() {
//			var result = null;
//			if( $('#text').val() == "" ) {
//				
//				result = null;
//			}else {
//				var memo = $('#text').val();
//				var params = "id="+$('#text').attr("memo_id")+"&content="+memo;  
//				
//				if($('#text').attr("memo_id") == undefined || $('#text').attr("memo_id") == null) {
//					alert('저장할 메모 탭을 먼저 클릭하세요!');
//				}else {
//					
//					if(memo.split("\n")[0].length < 10) {
//						$('.click').val(memo.split("\n")[0]);
//
//					}else {
//						$('.click').val(memo.slice(0,10));
//			
//					}
//					
//				}
//				result =  params;
//		
//			}
//			return result;
//			
//		};
	
		this.resetMemo = function() {
			
			var $textarea =  $('#text');
			$textarea.val("");
			
		};
		///////////////////////////////////////////////////////// 폴더 부분

		this.addFolder = function(addOnlyEvent,folder, event1, event2, event3, event4) {
			if(addOnlyEvent == null) {
				if(folder) {
					var $folderArticle = $('#folderArticle');
					this.$newFolder = $('<input>', {type :(folder.getFolderNum())? "button" : "text", id : (folder.getFolderNum())? folder.getFolderNum() : "newFolder", value : (folder.getFolderName())?  folder.getFolderName() : "새 폴더" ,class : "folderList"}).appendTo($folderArticle);
	
				}else {
					var $folderArticle = $('#folderArticle');
					this.$newFolder = $('<input>', {type : "text", id :  "newFolder", value :  "새 폴더"    ,class : "folderList"}).appendTo($folderArticle);
				}
			}
			
		
			if (event1) {
				this.$newFolder.on('click', event1); 
			}
			if (event2) {
				this.$newFolder.on('dblclick',event2);
			}
			if (event3) {
				this.$newFolder.on('focusout',event3);
			}
			if (event4) {
				this.$newFolder.one('focusout',event4); 
			}
		};
		////////////////////////////////////
		this.selectFolder = function(folderBtn, removeBtnEvent) {  
			console.dir(folderNum);
			$('.folderclick').removeClass("folderclick");
			$(folderBtn).addClass("folderclick");  //선택 스타일 변경
			$('.removeFolderBtn').remove();
			
			_this.removeText();
			$('.removeFolderBtn').remove();
			
			var folderNum = $('.folderclick').attr("id");  //폴더 번호 할당
			this.$removeFolder = $('<input>', {type : "button", id :  "removeFolderBtn", value :  "X", class : "removeFolderBtn", dataTarget : folderNum/*visibility : "hidden" */}).insertAfter($(folderBtn));
			console.dir(this.$removeFolder.attr("dataTarget"));
			this.$removeFolder.on('click',removeBtnEvent);    //삭제 버튼 생성
			
			
		};

		
		///////////////////이벤트 ///////////////
		this.setUpdateEvent = function(func) {

			this.$input1.on('click', func);
		};

		this.setLogoutEvent = function(func) {
			
			this.$input2.on('click', func);
		};
		
		this.setRemoveEvent = function(func) {
			
			this.$input3.on('click', func);
		};

		this.setCreateFolderEvent = function(func) {
			
			this.$createFolder.on('click', func);
			
		};
		this.setAllFolderEvent = function(func) {
			
			this.$allFolder.on('click', func);
			
		};
		this.setCreateMemoEvent = function(func) {
	
			this.$createMemo.on('click', func);
			
		};
		this.setResetEvent = function(func) {
			
			this.$reset.on('click', func);
			
		};
		this.setSaveMemoEvent = function(func) {
			
			this.$save.on('click', func);
			
		};
		
	};

	
	return	MemoView;
	
});